<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/5
 * Time: 9:39
 */

namespace app\course\controller;


use app\course\model\CourseCategoryModel;
use app\course\model\CourseModel;
use cmf\controller\AdminBaseController;

class CourseCategoryController extends AdminBaseController
{
    public function index()
    {

        $model = new CourseCategoryModel();
        $category_tree = $model->adminCategoryTableTree();
        $data = $model->where('delete_time', 0)->select();
        $this->assign('data', $data);
        $this->assign('category_tree', $category_tree);
        return $this->fetch();
    }

    /**
     * 添加课程页面
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    public function addPost()
    {

        $course_cate = new CourseCategoryModel();

        if ($this->request->isPost()) {
            $data = $this->request->post();
            //判断课程的名字是否存在
            $total = $course_cate->where('delete_time', '=', 0)
                ->where(['name' => trim($data['name'])])->count();
            if (isset($total) && $total >= 1) {
                $this->error('该课程类别已存在');
            }
        }
        $result = $this->validate($data, 'CourseCategory');
        if ($result !== true) {
            $this->error($result);
        }
        $course_cate->addCategory($data);
        //记录日志
        $id = $course_cate->getLastInsID(); //座位的id
        $pk = $id; //主键
        $table = 'cmf_course_category';//数据表名
        $content = lang('ADD_COURSE_CATEGORY', ['name' => $data['name']]);
        do_log($content, $pk, $table, time());

        $this->success('添加成功');


    }

    public function edit()
    {
        $id = $this->request->param('id');
        $model = new CourseCategoryModel();
        $data = $model::get($id)->toArray();
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
        }
        $model = new CourseCategoryModel();
        $result = $this->validate($data, 'CourseCategory');
        if ($result !== true) {
            $this->error($result);
        }
        $model->editCategory($data);

        //记录日志
        $id = intval($data['id']);
        $pk = $id; //主键
        $table = 'cmf_course_category';//数据表名
        $content = lang('EDIT_COURSE_CATEGORY', ['name' => $data['name']]);
        do_log($content, $pk, $table, time());
        $this->success('编辑成功', url('CourseCategory/index'));

    }

    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        $model = new CourseCategoryModel();
        $model_course = new CourseModel();

        $count = $model_course->where(['category_id' => $id, 'delete_time' => 0])->select()->count();
        if ($count > 0) {
            $this->error("有子类不能删除！", '');
        }
        $model->where('id', $id)->update(['delete_time' => time()]);
        //记录日志
        $data = $model->where('id', $id)->find();
        $pk = $id; //主键
        $table = 'cmf_course_category';//数据表名
        $content = lang('DELETE_COURSE_CATEGORY', ['name' => $data['name']]);
        do_log($content, $pk, $table, time());
        $this->success("删除成功！", '');
    }

}